import React from 'react';
import { KDlg, KInput, ck_empty, KFind, obj_int, obj_str, loc, ck_int, useKForm } from 'sui';

const DlgTask = (props) => {
    const form = useKForm(props.data);

    const ck = {
        dnum : v => {
            const e = ck_int(v);
            if (e) {
                return e;
            }

            const tmp = parseInt(v, 10);
            const count = obj_int(props.data, "dnum");

            if (tmp > count) {
                return loc("l_707");
            }

            return null;
        },
        cage : v => {
            const e = ck_empty(v);
            if (e) {
                return e;
            }

            const cage = props.cage;
            const f = cage.find(d => d.sn === v);
            if (!f) {
                return loc("l_528");
            }

            const num = obj_int(f, "num");
            const dnum = parseInt(form.get("dnum"), 10);

            if (dnum > num) {
                return loc("l_849");
            }

            return null;
        }
    };

    const col = [
        {name: loc("l_127"), key: 'num', width: 80},
    ];

    const getCage = () => {
        const cage = props.cage;
        const cur = props.data;

        if (!cage || !cur) {
            return null;
        }

        const ball = obj_str(cur, "ball");
        const ret = cage.filter(d => d.ball === ball);

        return ret;
    };

    return (
        <KDlg {...props} title={loc("l_706")} ck={ck} form={form}>
            <div className="text-base mb-2">{loc("l_802")}<span className="ml-8">{obj_str(props.data, "dw")}</span></div>
            <div className="text-base mb-2">{loc("l_721")}<span className="ml-8">{obj_str(props.data, "dnum")}</span></div>
            <KInput label={loc("l_129")} form={form} name="dnum"/>
            <KFind label={loc("l_528")} form={form} name="cage" list={getCage()} c1={loc("l_488")} c2={loc("l_848")} ext={col}/>
        </KDlg>
    );
};

export {DlgTask};
